package com.whisperarts.financer.app.dialogs;

import java.util.Calendar;
import java.util.Date;

import org.aspencloud.widgets.ACW;
import org.aspencloud.widgets.cdatepicker.CDatepickerCombo;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import com.whisperarts.financer.adapters.AccountAdapter;
import com.whisperarts.financer.adapters.CategoryAdapter;
import com.whisperarts.financer.app.widgets.AmountComposite;
import com.whisperarts.financer.model.account.Account;
import com.whisperarts.financer.model.transaction.Category;
import com.whisperarts.financer.model.transaction.Transaction;

/**
 * @author AD
 *
 */
public class AddTransactionDialog extends Dialog {

	private FormToolkit toolkit;

	private Shell shell;

	protected Combo comboAccounts;

	protected Combo comboCategories;

	private Transaction tx;

	private String note;

	private Account account;
	
	private Category category;
	
	private CDatepickerCombo timePicker;

	private CDatepickerCombo datePicker;

	private Text textNote;

	private final Calendar dateOfTx = Calendar.getInstance();

	protected AmountComposite amountComposite;
	
	protected AccountAdapter adapter;
	
	protected CategoryAdapter cAdapter;

	public AddTransactionDialog(Shell parentShell, Account account) {
		super(parentShell);
		this.account = account;
		adapter = new AccountAdapter();
		cAdapter = new CategoryAdapter();
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);		
		shell = newShell;
		shell.setText(account.getName());
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());

		Form form = toolkit.createForm(parent);
		form.setText("New transaction");
		toolkit.decorateFormHeading(form);
		toolkit.paintBordersFor(form.getBody());

		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 1;
		form.getBody().setLayout(layout);

		Composite composite = toolkit.createComposite(form.getBody());

		toolkit.adapt(composite);

		GridLayout glayout = new GridLayout(2, false);
		composite.setLayout(glayout);

		toolkit.createLabel(composite, "&Date:");
		Composite dateTimeComposite = toolkit.createComposite(composite);
		dateTimeComposite.setLayout(new GridLayout(2, true));
		
		datePicker = new CDatepickerCombo(dateTimeComposite, ACW.BORDER | ACW.DROP_DOWN);
		datePicker.getCDatepicker().setGridVisible(true);
		datePicker.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		timePicker = new CDatepickerCombo(dateTimeComposite, ACW.BORDER | ACW.TIME_SHORT);
		timePicker.setLayoutData(new GridData(GridData.END));		

		toolkit.createLabel(composite, "&Account:");
		comboAccounts = new Combo(composite, SWT.READ_ONLY);
		comboAccounts.setItems(adapter.getAccountNames());
		comboAccounts.select(adapter.getAccountPosition(account));
		comboAccounts.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				account = adapter.getAccountByPosition(comboAccounts.getSelectionIndex());
				shell.setText(account.getName());
			}
		});
		comboAccounts.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		toolkit.createLabel(composite, "A&mount:");		
		
		amountComposite = new AmountComposite(composite, SWT.NONE, account.getCurrency().getSymbol());
		amountComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		toolkit.createLabel(composite, "&Category:");

		comboCategories = new Combo(composite, SWT.READ_ONLY);
		comboCategories.setItems(cAdapter.getStructuredTitles(null));
		comboCategories.select(0);
		category = cAdapter.getCategoryById(0);
		comboCategories.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				category = cAdapter.getCategoryByPosition(comboCategories.getSelectionIndex());
				if (category.isIncome()) {
					amountComposite.setIncome(true);
				} else {
					amountComposite.setIncome(false);
				}
			}
		});
		comboCategories.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		toolkit.createLabel(composite, "&Note:");
		textNote = toolkit.createText(composite, "", SWT.BORDER);
		textNote.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		fillData();

		amountComposite.setFocus();
		return composite;
	}

	protected void fillData() {
	}

	@Override
	protected void okPressed() {		
		note = textNote.getText();
		Date date = datePicker.getSelection();
		Date time = timePicker.getSelection();
		dateOfTx.setTime(date);
		dateOfTx.add(Calendar.HOUR, time.getHours());
		dateOfTx.add(Calendar.MINUTE, time.getMinutes());
		
		createTransaction();
		super.okPressed();
	}

	protected void createTransaction() {
		tx = getTransactionToCreate();
		tx.setAccountId(account.getId());
		tx.setAmount(amountComposite.getAmount());
		tx.setCategory(category);
		tx.setIncome(amountComposite.isIncome());
		tx.setNote(note);		
		tx.setDate(dateOfTx);
	}

	protected Transaction getTransactionToCreate() {
		return new Transaction();
	}

	public Transaction getTransaction() {
		return tx;
	}

}
